#include <iostream>
#include <string>
#include <cstdlib>
#include <map>

#include "hunterAPI.hpp"
#include "hunterHTTP.hpp"

using namespace std;

int main()
{
	HunterAPI::HiveMind hm("./database");
	freopen("./log", "w+", stderr);

	string cookie = getCookie();
	string query  = getQuery();
	
	int id = -1;
	if (hm.verifyCookie(cookie, id) || id == -1)
	// login failed
	{
		cout << "Content-type: text/html;\n\n";
		o_header("Fail");
		o_login_error("Session expired w/ " + cookie);
		o_footer();
		return 0;
	}
	
	HunterAPI::Player pl;
	hm.getPlayerById(id, pl);

	HunterAPI::Hunt h;

	if (query == "win")
		// try to win
	{
		hm.getHuntById(pl.engaged, h);
		
		string s;
		getline(cin, s);
		map <string, string> par = HunterAPI::parseHTTPinput(s);
		
		cerr << "s is " << s << "\n";
		
		int r = 0;
		if (h.guys.first == pl.id)
			r = 1;
		else 
			r = 2;
					
		if (r == 1)
		{
			if (h.codes.second != par["code"])
				r = -1;
		}
		else if (r == 2)
		{
			if (h.codes.first != par["code"])
				r = -1;
		}
		
		if (r == -1)
			// wrong code
		{
			cout << "Content-type: text/html;\n\n";
			o_header("Fail");
			o_upper_visible(pl);
			cout << "<center><span style = \"color:red;font-weight:bold;\">Wrong code entered!</span><br>This incident will be reported.<br><a href = \"" << path_home << "/cgi-bin/submit.html\"> Return </a></center>";
			o_footer();
			return 0;
		}
		
		hm.closeHunt(h, r);
		
		cout << "Content-type: text/html;\n\n";
		o_header("Club -- Submit a result");
		o_upper_visible(pl);
		cout << "<center>Hunt is closed.</center>";
		o_footer();
		return 0;
	}
	
	if (query == "loss")
		// try to lose xD
	{
		hm.getHuntById(pl.engaged, h);
		int r = 0;
		if (h.guys.first == pl.id)
			r = 2;
		else
			r = 1;
			
		hm.closeHunt(h, r);
		
		cout << "Content-type: text/html;\n\n";
		o_header("Club -- Submit a result");
		o_upper_visible(pl);
		cout << "<center>Hunt is closed.</center>";
		o_footer();

		return 0;
	}

	// submit page goes here
	cout << "Content-type: text/html;\n\n";
	o_header("Club -- Submit a result");
	o_upper_visible(pl);
	cout << "<center>";

	if (pl.engaged > 0)
	{
		hm.getHuntById(pl.engaged, h);
		cout << "<script>function showCode() {dv = document.getElementById(\"info\"); if (dv.style.visibility == \"hidden\") {dv.style.visibility = \"visible\"; dv.style.height = \"\"; dv.innerHTML = \"My secret code is: <h3>" << ((pl.id == h.guys.first) ? h.codes.first : h.codes.second) << "</h3>\";} else {dv.style.visibility = \"hidden\"; dv.style.height = \"0px\";}} function subWin() {dv = document.getElementById(\"info\"); if (dv.style.visibility == \"hidden\") {dv.style.visibility = \"visible\"; dv.style.height = \"\"; dv.innerHTML = \"<form method = \'post\' action = \'" << path_home << "/cgi-bin/submit.cgi?query=win\'>Opponent's Secret Code: <input name = \'code\' type = \'text\'><br><input type = \'checkbox\'>I\'m no cheating <br><input type = \'submit\'></form>\";} else {dv.style.visibility = \"hidden\"; dv.style.height = \"0px\";}}  function subLos() {dv = document.getElementById(\"info\"); if (dv.style.visibility == \"hidden\") {dv.style.visibility = \"visible\"; dv.style.height = \"\"; dv.innerHTML = \"<form method = \'post\' action = \'" << path_home << "/cgi-bin/submit.cgi?query=loss\'>Are you sure? <br> <input type = \'submit\'></form>\";} else {dv.style.visibility = \"hidden\"; dv.style.height = \"0px\";}}</script>";
	}

	cout << "<table><tr><td></td><td>Status</td><td>" << ((pl.engaged <= 0) ? "<span style = \"color:black;\"> Not Engaged</span>" : "<span style = \"color:green;\">OK</span>") << "</td></tr><tr><td colspan = 3>";
	
	if (pl.engaged <= 0)
	{
		cout << "You are currently not engaged in any hunts. <br>Please, be patient and wait for several days; hunts will be launched when there will be some more contestants available.</td></tr>";
	}
	else
	{
		cout << "<center>You are <b>";
		if (h.guys.first == pl.id)
			cout << "hunter";
		else
			cout << "prey";
		cout << "</b></center></td></tr><tr><td><button onClick = 'javascript:showCode()'>Show/Hide Secret Code</button></td><td><button onClick = 'javascript:subWin()'>Submit My Win</button></td><td><button onClick = 'javascript:subLos()'>Leave This Hunt</button></td></tr>";
	}

	cout << "<tr><td colspan = 3><div id = \"info\" style = \"visibility:hidden; height:0px;\"></div></td></tr></table></center>";
	o_footer();
	
	return 0;
}
